import { Controller, Post, Body, BadRequestException } from "@nestjs/common";
import { AuthService } from "./auth.service";
class AuthDto {
  username: string;
  password: string;
}
@Controller("auth")
export class AuthController {
  constructor(private auth: AuthService) {}
  @Post("register") async register(@Body() dto: AuthDto) {
    if (!dto.username || !dto.password)
      throw new BadRequestException("Missing fields");
    return this.auth.register(dto.username, dto.password);
  }
  @Post("login") async login(@Body() dto: AuthDto) {
    if (!dto.username || !dto.password)
      throw new BadRequestException("Missing fields");
    const user = await this.auth.validateUser(dto.username, dto.password);
    if (!user) throw new BadRequestException("Invalid credentials");
    return this.auth.login(user);
  }
}
